package cn.tedu.store.product.webapi.controller;

import cn.tedu.store.commons.pojo.product.dto.AttributeAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.AttributeUpdateDTO;
import cn.tedu.store.commons.pojo.product.vo.AttributeListItemVO;
import cn.tedu.store.commons.pojo.product.vo.AttributeStandardVO;
import cn.tedu.store.product.service.IAttributeService;
import cn.tedu.store.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "02. 属性管理模块")
@Slf4j
@RestController
@RequestMapping("/attributes")
public class AttributeController {

    @Autowired
    IAttributeService attributeService;

    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AttributeAddNewDTO attributeAddNewDTO){
        log.info("开始处理[添加属性]请求 参数:{}",attributeAddNewDTO);
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.info("开始处理[删除属性]请求 参数:{}",id);
        attributeService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("设置销售属性")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/sale-type")
    public JsonResult<Void> setSaleType(@PathVariable Long id){
        log.debug("处理[设置为销售属性]请求 参数:{}",id);
        attributeService.setSaleType(id);
        return JsonResult.ok();
    }

    @ApiOperation("设置非销售属性")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/normal-type")
    public JsonResult<Void> setNormalType(@PathVariable Long id){
        log.debug("处理[设置为非销售属性]请求 参数:{}",id);
        attributeService.setNormalType(id);
        return JsonResult.ok();
    }

    @ApiOperation("设置允许自定义")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/allow-customize")
    public JsonResult<Void> setAllowCustomize(@PathVariable Long id){
        log.debug("处理[设置为允许自定义]请求 参数:{}",id);
        attributeService.setAllowCustomize(id);
        return JsonResult.ok();
    }

    @ApiOperation("设置禁止自定义")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/not-allow-customize")
    public JsonResult<Void> setNotAllowCustomize(@PathVariable Long id){
        log.debug("处理[设置为禁止自定义]请求 参数:{}",id);
        attributeService.setNotAllowCustomize(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改属性信息")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, AttributeUpdateDTO attributeUpdateDTO){
        log.debug("处理[修改属性信息]请求 参数:{}",id);
        attributeService.updateById(id,attributeUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("获取属性详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/get-by-id")
    public JsonResult<AttributeStandardVO> getStandardById(@PathVariable Long id){
        log.debug("处理[获取属性详情]请求 参数:{}",id);
        AttributeStandardVO result = attributeService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @ApiOperation("属性列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "templateId", value = "属性模板id", required = true, dataType = "long")
    @GetMapping("/{templateId:[0-9]+}/list-by-template")
    public JsonResult<List<AttributeListItemVO>> listByTemplateId(@PathVariable Long templateId){
        log.info("开始处理[根据属性模板查询属性列表]请求 参数:{}",templateId);
        return JsonResult.ok(attributeService.listByTemplateId(templateId));
    }

}